package com.yimixian.app.goods;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.OvershootInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yimixian.app.R;
import com.yimixian.app.common.WebViewActivity;
import com.yimixian.app.model.PromotionInfo;
import com.yimixian.app.util.UiUtils;

import java.util.List;

public class DialogEffectActivity extends Activity {

    private LinearLayout ll_cu_xiao, ll_anim;
    private RelativeLayout rl_main, rl_close;
    private ImageView iv_anim;
    public static int DIALOG_EFFECT_DATA = 9000;
    private OvershootInterpolator OverShootInterpolator = new OvershootInterpolator();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog_effect);

        if(UiUtils.effectBitmap == null){
            finish();
            overridePendingTransition(0, 0);
            return;
        }

        ll_cu_xiao = (LinearLayout) findViewById(R.id.ll_cu_xiao);
        ll_anim = (LinearLayout) findViewById(R.id.ll_anim);
        rl_main = (RelativeLayout) findViewById(R.id.rl_main);
        rl_close = (RelativeLayout) findViewById(R.id.rl_close);
        iv_anim = (ImageView) findViewById(R.id.iv_anim);

        List<PromotionInfo> infos = (List<PromotionInfo>) getIntent().getSerializableExtra("data");

        iv_anim.setImageBitmap(UiUtils.effectBitmap);

        float density = getResources().getDisplayMetrics().density;
        if(infos != null){
            ll_cu_xiao.setVisibility(View.VISIBLE);
            for (int i = 0; i < infos.size(); i++) {

                final PromotionInfo info = infos.get(i);
                RelativeLayout rl_row = new RelativeLayout(this);
                rl_row.setBackgroundResource(R.drawable.item_white_selector);

                TextView tv_title = new TextView(this);
                tv_title.setId(R.id.title_text);
                tv_title.setText(info.title);
                tv_title.setTextColor(getResources().getColor(R.color.ymx_orange));
                tv_title.setBackgroundResource(R.drawable.te_hui_back);
                tv_title.setTextSize(13);
                tv_title.setPadding((int) (2 * density), (int) (2 * density), (int) (2 * density), (int) (2 * density));
                RelativeLayout.LayoutParams lp_title = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                lp_title.addRule(RelativeLayout.CENTER_VERTICAL);

                TextView tv_desc = new TextView(this);
                tv_desc.setText(info.desc);
                tv_desc.setTextColor(0xff292929);
                tv_desc.setTextSize(13);
                tv_desc.setPadding((int) (7 * density), 0, 0, 0);
                tv_desc.setSingleLine(true);
                tv_desc.setEllipsize(TextUtils.TruncateAt.END);
                RelativeLayout.LayoutParams lp_desc = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                lp_desc.addRule(RelativeLayout.CENTER_VERTICAL);
                lp_desc.addRule(RelativeLayout.RIGHT_OF, R.id.title_text);

                rl_row.addView(tv_title, lp_title);
                rl_row.addView(tv_desc, lp_desc);

                LinearLayout.LayoutParams lp_row = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

                ll_cu_xiao.addView(rl_row, lp_row);

                if(i == infos.size() - 1){
                    rl_row.setPadding(0, (int) (12 * density), 0, (int) (12 * density));
                }else{
                    rl_row.setPadding(0, (int) (12 * density), 0, (int) (12 * density));
                    View view_line = new View(this);
                    view_line.setBackgroundColor(0xffeeeeee);
                    ll_cu_xiao.addView(view_line, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
                }

                final int index = i;
                rl_row.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(DialogEffectActivity.this, WebViewActivity.class);
                        intent.putExtra(WebViewActivity.EXTRA_URL, info.url);
                        startActivity(intent);
                    }
                });

            }
        }

        rl_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                close2();
            }
        });

        startEffect2();
    }

    private void startEffect1(){
        AnimationSet as = new AnimationSet(true);
        ScaleAnimation scaleOne = new ScaleAnimation(1.2f, 1.0f, 1.2f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        AlphaAnimation alpha = new AlphaAnimation(0f, 1f);
        as.addAnimation(scaleOne);
        as.addAnimation(alpha);
        as.setDuration(600);
        as.setStartOffset(100);
        as.setInterpolator(OverShootInterpolator);
        ll_anim.startAnimation(as);

        ScaleAnimation scaleTwo = new ScaleAnimation(1.0f, 0.9f, 1.0f, 0.9f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleTwo.setDuration(600);
        scaleTwo.setStartOffset(100);
        scaleTwo.setFillAfter(true);
        scaleTwo.setInterpolator(OverShootInterpolator);
        iv_anim.startAnimation(scaleTwo);
    }

    private void close1(){

        AnimationSet as = new AnimationSet(true);
        ScaleAnimation scaleOne = new ScaleAnimation(1.0f, 1.2f, 1.0f, 1.2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        AlphaAnimation alpha = new AlphaAnimation(1f, 0f);
        as.addAnimation(scaleOne);
        as.addAnimation(alpha);
        as.setDuration(500);
        as.setInterpolator(OverShootInterpolator);
        ll_anim.startAnimation(as);

        ScaleAnimation scaleTwo = new ScaleAnimation(0.9f, 1.0f, 0.9f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleTwo.setDuration(500);
        scaleTwo.setInterpolator(OverShootInterpolator);
        iv_anim.startAnimation(scaleTwo);

        as.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                finish();
                overridePendingTransition(0, 0);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
    }

    private void startEffect2(){

        AnimationSet as = new AnimationSet(true);
        TranslateAnimation trans = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 0f);
        AlphaAnimation alpha = new AlphaAnimation(0f, 1f);
        as.addAnimation(trans);
        as.addAnimation(alpha);
        as.setDuration(300);
        ll_anim.startAnimation(as);

        AnimatorSet set = new AnimatorSet();
        ObjectAnimator anim1 = ObjectAnimator.ofFloat(iv_anim, "rotationX", new float[]{0f, -5f});
        ObjectAnimator anim2 = ObjectAnimator.ofFloat(iv_anim, "scaleX", new float[]{1f, 0.9f});
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(iv_anim, "scaleY", new float[]{1f, 0.9f});
        set.setDuration(300);
        set.playTogether(anim1, anim2, anim3);
        set.addListener(new Animator.AnimatorListener() {

            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                ObjectAnimator anim = ObjectAnimator.ofFloat(iv_anim, "rotationX", new float[]{-5f, 0f});
                anim.setDuration(150);
                anim.start();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }
        });
        set.start();
    }

    private void close2(){

        AnimationSet as = new AnimationSet(true);
        TranslateAnimation trans = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f);
        AlphaAnimation alpha = new AlphaAnimation(1f, 0f);
        as.addAnimation(trans);
        as.addAnimation(alpha);
        as.setDuration(300);
        ll_anim.startAnimation(as);

        ScaleAnimation scaleTwo = new ScaleAnimation(0.9f, 1.0f, 0.9f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleTwo.setDuration(300);
        scaleTwo.setInterpolator(OverShootInterpolator);
        iv_anim.setScaleX(1f);
        iv_anim.setScaleY(1f);
        iv_anim.startAnimation(scaleTwo);

        as.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                finish();
                overridePendingTransition(0, 0);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
    }

    public void onBackPressed() {
        close2();
    }
}
